﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace HDT.GUI
{
    public partial class FrmCauHinh : Form
    {
        public delegate void myDelegate();
        public myDelegate loadMain;

        private string infoPath = "DatabaseProperties.xml";
        public FrmCauHinh()
        {
            InitializeComponent();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LoadConnectionInfo()
        {
            XmlDocument docXML = new XmlDocument();
            docXML.Load(infoPath);
            txtHost.Text = docXML.SelectSingleNode("//ServerName").InnerText;
            txtTenCSDL.Text = docXML.SelectSingleNode("//DatabaseName").InnerText;
            txtUserCSDL.Text = docXML.SelectSingleNode("//Username").InnerText;
            txtMatKhau.Text = docXML.SelectSingleNode("//Password").InnerText;
        }

        private void btnNhapLai_Click(object sender, EventArgs e)
        {
            LoadConnectionInfo();
            btnNhapLai.Enabled = false;
            btnLuu.Enabled = false;
        }

        public void SetConnectionInfo()
        {
            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                XmlWriter writer = XmlWriter.Create(infoPath, settings);
                writer.WriteStartDocument();
                writer.WriteComment("This file is my database connection info");
                writer.WriteStartElement("DatabaseInfo");
                writer.WriteElementString("ServerName", txtHost.Text.Trim());
                writer.WriteElementString("DatabaseName", txtTenCSDL.Text.Trim());
                writer.WriteElementString("Username", txtUserCSDL.Text.Trim());
                writer.WriteElementString("Password", txtMatKhau.Text);
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
            }
            catch (Exception)
            {
                txtHost.Clear();
                txtUserCSDL.Clear();
                txtTenCSDL.Clear();
                txtMatKhau.Clear();
            }
        }

        private void FrmCauHinh_Load(object sender, EventArgs e)
        {
            LoadConnectionInfo();
            btnNhapLai.Enabled = false;
            btnLuu.Enabled = false;
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            bool status = !("".Equals(txtHost.Text) || "".Equals(txtUserCSDL.Text) || "".Equals(txtMatKhau.Text));
            btnLuu.Enabled = status;
            btnNhapLai.Enabled = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                SetConnectionInfo();
                MessageBox.Show("Cập nhật thành công. Bạn phải khởi động lại chương trình để áp dụng", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Dispose();
                loadMain();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "LỖI", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
